home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / write_sylk.pro < prev    next >
Text File  |  1997-07-08  |  7KB  |  221 lines

  1. ; $Id: write_sylk.pro,v 1.3 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1994-1997, Research Systems, Inc.  All rights reserved.
  4. ;    Unauthorized reproduction prohibited.
  5.  
  6.  
  7. ; Writes a single line of sylk cell data to file.
  8.  
  9. PRO WriteSylkCell, lunOutfile, Data, iRow, iCol
  10.  
  11.     ON_ERROR, 2
  12.  
  13.     ; If the data is anything but a string, simply write it to file.
  14.     IF ((SIZE(Data))[1] NE 7) THEN BEGIN
  15.         PRINTF, lunOutfile, "C;X", STRCOMPRESS(STRING(iCol), /REMOVE_ALL), $
  16.             ";Y", STRCOMPRESS(STRING(iRow), /REMOVE_ALL), ";K", $
  17.             STRCOMPRESS(STRING(Data), /REMOVE_ALL)
  18.  
  19.     ; Otherwise, surround the string in double quotes.
  20.     ENDIF ELSE BEGIN
  21.         PRINTF, lunOutfile, "C;X", STRCOMPRESS(STRING(iCol), /REMOVE_ALL), $
  22.             ";Y", STRCOMPRESS(STRING(iRow), /REMOVE_ALL), ";K", '"', $
  23.             STRCOMPRESS(STRING(Data), /REMOVE_ALL), '"'
  24.     ENDELSE
  25.  
  26.     RETURN
  27. END
  28.  
  29.  
  30. FUNCTION WRITE_SYLK, Outfile, SourceData, STARTROW = iStartRow, STARTCOL = iStartCol
  31.  
  32. ;
  33. ;+
  34. ; NAME:
  35. ;   WRITE_SYLK
  36. ;
  37. ; PURPOSE:
  38. ;   Writes the contents of an IDL variable to a sylk (Symbolic Link) format 
  39. ;   spreadsheet data file. 
  40. ;
  41. ; CATEGORY:
  42. ;   Input/Output.
  43. ;
  44. ; CALLING SEQUENCE:
  45. ;   fStatus = WRITE_SYLK(OutFile, SourceData [, STARTROW, STARTCOL])
  46. ;
  47. ; INPUT:
  48. ;   OutFile: Scalar string with the name of the sylk file to write.
  49. ;   SourceData: A scalar, a vector, or a 2D array to be written to file.
  50. ;
  51. ; OUTPUT:
  52. ;   fStatus: Boolean flag.  Returns TRUE if function was successful. 
  53. ;
  54. ; OPTIONAL INPUT PARAMETERS:
  55. ;   STARTROW: The starting (0-based) row of spreadsheet cells to which the 
  56. ;       routine will write the data.  If not specified, this value defaults 
  57. ;       to row 0. 
  58. ;   STARTCOL: The starting (0-based) column of spreadsheet cells to which the
  59. ;       routine will write the data.  If not specified, this value defaults 
  60. ;       to column 0.
  61. ;
  62. ; SIDE EFFECTS:
  63. ;   None.
  64. ;
  65. ; RESTRICTIONS:
  66. ;   This routine *only* writes numerical and string sylk data.  It connot
  67. ;   handle spreadsheet and cell formatting information such as cell width, text
  68. ;   justification, font type, date, time, and monetary notations, etc.  A 
  69. ;   particular sylk data file cannot be appended with data blocks through 
  70. ;   subsequent calls.
  71. ;
  72. ; EXAMPLES:
  73. ;   Assume you wished to write the contents of a 2x2 array of floats, 
  74. ;   arrfltData, to a sylk data file called "bar.slk" such that, when read into 
  75. ;   a spreadsheet, the matrix would appear with it's upper left data at the 
  76. ;   cell in the 10th row and the 20th column.  The following call would 
  77. ;   accomplish this task:
  78. ;   
  79. ;       fStatus = WRITE_SYLK("bar.slk", arrflData, STARTROW = 9, STARTCOL = 19)
  80. ;
  81. ;
  82. ; MODIFICATION HISTORY:
  83. ;   Written October 1994, AJH
  84. ;-
  85. ; Copyright (c) 1994, Research Systems, Inc.  All rights reserved.
  86. ;   Unauthorized reproduction prohibited.
  87. ;
  88.     
  89.     ON_ERROR, 2
  90.     ON_IOERROR, CleanUp
  91.  
  92.     _BAD_ =    0B
  93.     _SCALAR_ = 1B
  94.     _VECTOR_ = 2B
  95.     _MATRIX_ = 3B
  96.     _TABLE_ =  4B
  97.  
  98.     typeData = _BAD_
  99.     lunOutfile = 0
  100.     fStatus = 0
  101.  
  102.     ; First check to see if the correct number of positional parameters have
  103.     ; been passed
  104.     IF (N_PARAMS() NE 2) THEN BEGIN
  105.         MESSAGE, "Calling sequence - WRITE_SYLK, Outfile, SourceData, " + $
  106.             "STARTROW, STARTCOL", /CONTINUE
  107.         GOTO, CleanUp
  108.     ENDIF
  109.  
  110.     ; Check the validity of the file parameter
  111.     IF (N_ELEMENTS(Outfile) EQ 0) THEN BEGIN
  112.         MESSAGE, "Error - A STRING filename must be passed in the Outfile " + $
  113.             "parameter.", /CONTINUE
  114.         GOTO, CleanUp
  115.     ENDIF
  116.  
  117.     ; Check the validity and type of the SourceData parameter
  118.     IF (N_ELEMENTS(SourceData) NE 0) THEN BEGIN
  119.         sizeSourceData = SIZE(SourceData)
  120.         CASE sizeSourceData[0] OF
  121.             0:      typeData = _SCALAR_
  122.             1:      BEGIN
  123.                         typeData = _VECTOR_
  124.                         IF (sizeSourceData[2] EQ 8) THEN BEGIN
  125.                             typeData = _TABLE_
  126.                         ENDIF
  127.                     END
  128.             2:      BEGIN
  129.                         typeData = _MATRIX_
  130.                         IF (sizeSourceData[3] EQ 8) THEN BEGIN
  131.                             typeData = _BAD_
  132.                         ENDIF
  133.                     END
  134.             ELSE:   typeData = _BAD_
  135.         ENDCASE
  136.     ENDIF
  137.  
  138.     IF (typeData EQ _BAD_) THEN BEGIN
  139.         MESSAGE, "Error - Either a scalar, a vector, or a 2D ARRAY of " + $
  140.             "scalars must be passed in the SourceData parameter.", /CONTINUE
  141.         GOTO, CleanUp
  142.     ENDIF
  143.  
  144.     ; Setup keyword default values.
  145.     IF (N_ELEMENTS(iStartRow) EQ 0) THEN BEGIN
  146.         iStartRow = 0
  147.     ENDIF ELSE BEGIN
  148.         iStartRow = (iStartRow > 0)
  149.     ENDELSE
  150.  
  151.     IF (N_ELEMENTS(iStartCol) EQ 0) THEN BEGIN
  152.         iStartCol = 0
  153.     ENDIF ELSE BEGIN
  154.         iStartCol = (iStartCol > 0)
  155.     ENDELSE
  156.  
  157.     IF (N_ELEMENTS(fUpdate) EQ 0) THEN fUpdate = 0
  158.     
  159.     ; If Outfile is a filename, open it for reading and get its lun
  160.     IF ((SIZE(Outfile))[1] EQ 7) THEN BEGIN
  161.         OPENW, lunOutfile, Outfile, /GET_LUN, ERROR = fOpenWrite
  162.         IF (fOpenWrite NE 0) THEN BEGIN
  163.             MESSAGE, "Error - File " + STRCOMPRESS(Outfile, /REMOVE_ALL) + $
  164.                 " cannot be opened.", /CONTINUE
  165.             GOTO, CleanUp           
  166.         ENDIF
  167.         fstatResult = FSTAT(lunOutfile)
  168.         IF (fstatResult.WRITE EQ 0) THEN BEGIN
  169.             MESSAGE, "Error - File " + STRCOMPRESS(Outfile, /REMOVE_ALL) + $
  170.                 " cannot be written to.", /CONTINUE
  171.             GOTO, CleanUp
  172.         ENDIF
  173.     ENDIF
  174.  
  175.     ; Write the SYLK file creation app id to file.
  176.     PRINTF, lunOutfile, "ID;PIDL"
  177.     
  178.     CASE typeData OF
  179.         _SCALAR_:   WriteSylkCell, lunOutfile, SourceData, iStartRow + 1, $
  180.                         iStartCol + 1   
  181.         _VECTOR_:   BEGIN
  182.                         FOR i = 0, sizeSourceData[1] - 1 DO BEGIN
  183.                             WriteSylkCell, lunOutfile, SourceData[i], $
  184.                                 i + iStartRow + 1, iStartCol + 1
  185.                         ENDFOR
  186.                     END
  187.         _MATRIX_:   BEGIN
  188.                         FOR i = 0, sizeSourceData[1] - 1 DO BEGIN
  189.                             FOR j = 0, sizeSourceData[2] - 1 DO BEGIN
  190.                                 WriteSylkCell, lunOutfile, SourceData[i, j], $
  191.                                     i + iStartRow + 1, j + iStartCol + 1
  192.                             ENDFOR
  193.                         ENDFOR
  194.                     END
  195.         _TABLE_:    BEGIN
  196.                         nTags = N_TAGS(SourceData)
  197.                         FOR i = 0, sizeSourceData[1] - 1 DO BEGIN
  198.                             FOR j = 0, nTags - 1 DO BEGIN
  199.                                 WriteSylkCell, lunOutfile, $
  200.                                     SourceData[i].(j), i + iStartRow + 1, $
  201.                                     j + iStartCol + 1
  202.                             ENDFOR
  203.                         ENDFOR
  204.                     END
  205.     ENDCASE
  206.     
  207.     ; Write the SYLK end-of-data descriptor.
  208.     PRINTF, lunOutfile, "E"
  209.     fStatus = 1
  210.     
  211.     GOTO, CleanUp
  212.  
  213.     CleanUp: BEGIN
  214.         IF (lunOutfile NE 0) THEN BEGIN
  215.             FREE_LUN, lunOutfile
  216.         ENDIF
  217.     END
  218.  
  219.     RETURN, fStatus
  220. END
  221.